package handler

import (
	"context"
	"errors"
	pb "mpps/services/srv-UserRefreshToken/proto"
	"mpps/tools/auth"
	"mpps/tools/common"

	"github.com/asim/go-micro/v3/logger"
)

type UserRefreshToken struct {
}

func NewUserRefreshToken() *UserRefreshToken {
	return &UserRefreshToken{}
}

// Call is a single request handler called via client.Call or the generated client code
func (c *UserRefreshToken) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
	if len(req.AccessToken) <= 0 || len(req.RefreshToken) <= 0 {
		return errors.New(common.ErrParamsValidation.Message)
	}
	// 根据老token生成新token
	accessToken, refreshToken, err := auth.UpdateToken(req.AccessToken, req.RefreshToken)
	if err != nil {
		logger.Info(err)
		return errors.New(common.ErrAuth.Message)
	}
	rsp.AccessToken = accessToken
	rsp.RefreshToken = refreshToken
	return nil
}
